package com.bbc.yuntun.platform.utils;

import com.bbc.yuntun.common.utils.ServletUtils;
import com.bbc.yuntun.common.utils.spring.SpringUtils;
import com.bbc.yuntun.platform.domain.model.WxLoginUser;
import com.bbc.yuntun.platform.domain.WxUser;
import com.bbc.yuntun.platform.service.wx.WxTokenService;

import java.util.Objects;

/**
 * 微信用户工具类
 *
 * @author wl
 * @date 2023/12/8 11:00
 */
public class WxUserUtils {

    private static volatile WxTokenService wxTokenService;

    /**
     * 获取单例WxTokenService
     *
     * @author wl
     * @date 2023/12/8 11:07
     * @return com.bbc.yuntun.platform.service.wx.WxTokenService
     */
    private static WxTokenService getWxTokenService() {
        if (Objects.isNull(wxTokenService)) {
            synchronized (WxTokenService.class) {
                if(Objects.isNull(wxTokenService)) {
                    wxTokenService = SpringUtils.getBean(WxTokenService.class);
                }
            }
        }
        return wxTokenService;
    }

    /**
     * 获取当前微信登录用户
     *
     * @return WxLoginUser
     */
    public static WxLoginUser getWxLoginUser() {
        return getWxTokenService().getLoginUser(ServletUtils.getRequest());
    }

    /**
     * 获取当前小程序用户信息
     *
     * @author wl
     * @date 2023/12/8 11:13
     * @return com.bbc.yuntun.platform.domain.WxUser
     */
    public static WxUser getWxUser() {
        return getWxLoginUser().getWxUser();
    }

    /**
     * 获取当前小程序用户ID
     *
     * @author wl
     * @date 2023/12/8 11:13
     * @return java.lang.Long
     */
    public static Long getWxUserId() {
        return getWxUser().getId();
    }

}
